<template>
	<view>
		<st-mc-framework navTitle="古越电源注塑MES" @changeLevel="changeLevel">
			<template #content>
				<view class="contentBox content-top-Box">
					<view class="content-top-Box-view">
						装配1(组装)现场质量管理巡检确认表(1)</view>
					<u-row style="background-color: #fff;padding-top: 5rpx;">
						<u-col span="9">
							<view class="demo-layout bg-purple-light ">
								<text class="colText colText_child"> 日期：{{mainObject.OperateDate}}</text>
							</view>
						</u-col>
						<u-col span="3">
							<view class="demo-layout bg-purple">
								<text class="colText">控制号：Q/GY-QP034-05</text>
							</view>
						</u-col>
					</u-row>
					<view class="table">
						<table style="table-layout: fixed;">
							<tr>
								<th width='5%'>序号</th>
								<th width='8%'>工序</th>
								<th width='30%'>检查内容</th>
								<th width='10%'>巡检日期</th>
								<th>检查情况</th>
								<th width='10%'>巡检时间</th>
								<th>检查情况</th>
								<th>异常处置</th>
							</tr>
							<tbody>
								<tr v-for="(item,index) in List" :key="index">
									<td>{{index+1}}</td>

									<td v-show='item.count==1'>
										<view class="ListTdDiv">
											<text>{{item.ProcedureName}}</text>
										</view>
									</td>
									<td v-show='item.count>1' :rowspan='item.count'>
										<view class="ListTdDiv">
											<text>{{item.ProcedureName}}</text>
										</view>
									</td>
									<td>
										<view class="newlineDiv">
											<text>{{item.InspectionContent||item.ProgramContent}}</text>
										</view>
									</td>

									<td>
										<view @click="addInspectionDate('日期1',index)" class="newlineDiv  newlineDiv1">
											<text>{{item.InspectionDate1}}</text>
										</view>
									<td>
										<u--textarea :disabled='readOnlyShow' v-model="item.InspectionSituation1"
											autoHeight border='none' style="background-color: #e7e7e7"></u--textarea>
									<td>
										<view class="newlineDiv newlineDiv1" @click="addInspectionDate('日期2',index)">
											<text>{{item.InspectionDate2}}</text>
										</view>
									</td>
									<td>
										<u--textarea :disabled='readOnlyShow' v-model="item.InspectionSituation2"
											autoHeight border='none' style="background-color: #e7e7e7"></u--textarea>
									</td>
									<td>
										<u--textarea :disabled='readOnlyShow' v-model="item.InspectionYCSituation"
											autoHeight border='none' style="background-color: #e7e7e7"></u--textarea>
									</td>
								</tr>
							</tbody>
						</table>
					</view>
					<view class="contentBox_bottom">
						<view class='contentBox_bottom_view1'>注</view> 1、此表由巡检员填写，每天按检查内容逐项对铸焊装配质量、工艺符合度进行检查确认;
					</view>
					<view class="contentBox_bottom">
						<view class='contentBox_bottom_view1'></view> 2、检查符合的打√，不符合的写上不良情况，及时纠正并报告，反馈主管车间主任处置及整改。
					</view>
					<view class="contentBox_bottom">
						<view class='contentBox_bottom_view2'>
							<view class="bottom_view2_view"><text>检查人：</text><input type="text"
									v-model="mainObject.InspectionMan" disabled /></view>
							<view class="bottom_view2_view"><text>确认人：</text><input type="text"
									v-model="mainObject.QRMan" disabled /></view>


						</view>
					</view>
					<view class="DetailsbottomBtnsTow" style="margin-top: 15px;">
						<button block class="button1" @click="submit('保存')">
							保存
						</button>

						<button v-if='ApproveBack("审核")' class="button2" block @click="toexamine" v-show="buttonShow">
							审核
						</button>
					</view>

				</view>

				<u-toast ref="uToast"></u-toast>

				<u-popup :show="popup_logout" mode="center" :closeOnClickOverlay="false" :round="4">
					<view class="popup-content">
						<view class="popup-content_close x-c">
							<uni-icons type="closeempty" color="#999" size="20"
								@click='popup_logout = false'></uni-icons>
						</view>
						<view class="x-c title">
							<text>{{popuptitle}}</text>
						</view>
						<view class="contentView">
							<text>{{popupcontent}}</text>
						</view>
						<view class="btnBox x-ac">
							<u-button v-show="popuptitle=='审核'" type="info" text="取消"
								@click='popup_logout=false'></u-button>
							<u-button type="primary" text="确定" @click="confirmClick"></u-button>
						</view>
					</view>
				</u-popup>
				<u-datetime-picker :show="Timeshow" v-model="value1" mode="datetime" @cancel='Timeshow = false'
					@close="Timeshow=false" @confirm='pickerConfirm'></u-datetime-picker>
			</template>
		</st-mc-framework>
		<u-loading-page :loading="loadingPage" class="loading-page" :loading-text="loadingName"></u-loading-page>
	</view>
</template>

<script>
	import {
		getTimeStamp,
		getFormatDate,
		getFormatDateTime,
		getBefore7DateTime,
		formatDateTime,
		navigateMixin,
		baseAddModuleData_select,
		getchangeLevel
	} from '@/st_mes_zs/common/base.js';
	
	export default {
		mixins: [navigateMixin, baseAddModuleData_select],
		data() {
			return {
				readOnlyShow: false,

				popuptitle: '',
				popupcontent: '',
				popup_logout: false,
				buttonShow: false,
				modalcontent: '',
				comentIndex: null,
				loadingPage: false,
				loadingName: '',
				TimeName: '',
				Timeshow: false,
				TimeIndex: null,
				value1: Number(new Date()),

				List: [],
				mainObject: {
					OperateDate: '',
					QRMan: '',
					InspectionMan: '',
					PatrolInspectionConfirmID: 0
				},
				BtnApprove: []
			};
		},
		onLoad(onlod) {
			let that = this;
			console.log(this.$store.state.appConfigInfo.ModuleInfo_All);
			let foundNode = null;
			for (const module of this.$store.state.appConfigInfo.ModuleInfo_All) {
				if (module.TreeTypeName === '生产检验') {
					for (const node of module.Node) {
						if (node.APFunName === '巡检确认') {
							foundNode = JSON.parse(JSON.stringify(node));
							break; // 找到后立即退出内层循环  
						}
					}
					if (foundNode) {
						break;
					}
				}
			}
			that.BtnApprove = foundNode['IconApprove'];


			that.$store.state.GuyueModule.MesBasicInfo.Status.ClockStatusTitle = '其他'
			console.log(onlod);

			that.loadingPage = true
			that.loadingName = '正在加载中'
			if (onlod.PatrolInspectionConfirmID) {
				setTimeout(() => {
					that.queryList(onlod.PatrolInspectionConfirmID)
				}, 500)
			}

		},
		onShow() {

		},
		onBackPress(options) { // 监听页面返回
			if (options.from == 'navigateBack') { // 通过点击路由返回
				return false;
			} else if (options.from == 'backbutton') { // 通过外置返回
				return true;
			}
		},
		methods: {
			confirmClick() {
				this.popup_logout = false
				if (this.popuptitle == '审核') {
					this.submit('审核')
				}
			},

			toexamine() {
				var that = this
				that.popuptitle = '审核'
				that.popupcontent = '确定审核吗？'
				that.popup_logout = true
			},
			ApproveBack(str) {
				let that = this;
				let Type = false;
				if (that.BtnApprove.length) {
					Type = that.BtnApprove.some(item => {
						return item.MenuName == str
					})
				}
				return Type;
			},




			submit(str) {
				var that = this;
				var show = true
				var msg

				console.log(that.mainObject);
				if (that.mainObject.InspectionMan == '' || that.mainObject.InspectionMan == null) {
					msg = '请选择检查人'
					show = false
				} else if (that.mainObject.QRMan == '' || that.mainObject.QRMan == null) {
					msg = '请选择确认人'
					show = false
				}

				if (show) {
					var list = JSON.parse(JSON.stringify(that.List))
					list.forEach(item => {
						if (item.InspectionDate1 == '' || item.InspectionDate1 == null) {
							item.InspectionDate1 = '1900-01-01T00:00:00'
						}
						if (item.InspectionDate2 == '' || item.InspectionDate2 == null) {
							item.InspectionDate2 = '1900-01-01T00:00:00'
						}
						if (item.InspectionSituation2 == '' || item.InspectionSituation2 == null) {
							item.InspectionSituation2 = '@'
						}
						if (item.InspectionYCSituation == '' || item.InspectionYCSituation == null) {
							item.InspectionYCSituation = '@'
						}
						if (item.InspectionSituation1 == '' || item.InspectionSituation1 == null) {
							item.InspectionSituation1 = '@'
						}
					})

					var obj = {
						PatrolInspectionConfirmDeID: list.map(item => item.PatrolInspectionConfirmDeID).join('~'),
						InspectionDate1: list.map(item => item.InspectionDate1).join('~'),
						InspectionDate2: list.map(item => item.InspectionDate2).join('~'),
						InspectionSituation2: list.map(item => item.InspectionSituation2).join('~'),
						InspectionYCSituation: list.map(item => item.InspectionYCSituation).join('~'),
						InspectionSituation1: list.map(item => item.InspectionSituation1).join('~'),
						QRMan: that.mainObject.QRMan,
						InspectionMan: that.mainObject.InspectionMan,
						PatrolInspectionConfirmID: that.mainObject.PatrolInspectionConfirmID,
					}
					console.log(obj);
					that.loadingName = '正在保存中'
					that.loadingPage = true
					that.Request.request('savePatrolInspectionDetail', obj, function(ret) {
						console.log(JSON.stringify(ret));
						if (ret.status == 200) {
							that.loadingPage = false
							that.$refs.uToast.show({
								title: '保存',
								type: 'success',
								message: '保存成功'
							})

							if (str == '审核') {
								var obj1 = {
									Type: 'XJQR',
									ID: ret.data.ds[0].PatrolInspectionConfirmID
								}
								that.Request.request('ApproveXJConfirm', obj1, (res) => {
									if (res.status == 200) {
										that.$refs.uToast.show({
											title: '审核',
											type: 'success',
											message: '审核成功'
										})
										setTimeout(() => {
											that.queryList(ret.data.ds[0]
												.PatrolInspectionConfirmID)
										}, 1000)
									}

								})
							} else {
								setTimeout(() => {
									that.queryList(ret.data.ds[0].PatrolInspectionConfirmID)
								}, 1000)
							}
						}
					})

				} else {
					that.loadingPage = false


					that.popuptitle = '报错'
					that.popupcontent = msg
					that.popup_logout = true


				}
			},

			addInspectionDate(item, index) {
				let that = this;
				if (!that.readOnlyShow) {
					that.value1 = Number(new Date())
					that.TimeName = item
					that.TimeIndex = index
					that.Timeshow = true

				}

			},
			pickerConfirm(e) {
				let that = this;
				var time = getFormatDate(new Date(e.value))
				console.log(time);
				if (that.TimeName == '日期1') {
					that.List[that.TimeIndex].InspectionDate1 = time
				} else {
					that.List[that.TimeIndex].InspectionDate2 = time
				}
				that.Timeshow = false
				that.$forceUpdate()
			},

			queryList(id) {
				var that = this

				try {
					that.Request.request('getPatrolInspectionDetail', {
						PatrolInspectionConfirmID: id
					}, (ret) => {
						console.log(ret.data);
				
						ret.data.ds1.forEach((obj, index) => {
							obj.count = 0
							obj.PatrolInspectionConfirmDeID = obj.PatrolInspectionConfirmDeID ? obj
								.PatrolInspectionConfirmDeID : 0
							if (obj.InspectionDate1 == '1900-01-01T00:00:00') {
								obj.InspectionDate1 = ''
							} else {
								obj.InspectionDate1 = obj.InspectionDate1 ? obj.InspectionDate1.replace(
									'T', ' ').split('.')[0] : ''
							}

							if (obj.InspectionDate2 == '1900-01-01T00:00:00') {
								obj.InspectionDate2 = ''
							} else {
								obj.InspectionDate2 = obj.InspectionDate2 ? obj.InspectionDate2.replace(
									'T', ' ').split('.')[0] : ''
							}
				
						});
						let currentGroup = {
							name: ret.data.ds1[0].ProcedureName,
							startIndex: 0,
							count: 1
						};
						// 遍历数组（从第二个元素开始）
						for (let i = 1; i <= ret.data.ds1.length; i++) {
							const currentName = i < ret.data.ds1.length ? ret.data.ds1[i].ProcedureName : null;

							// 如果当前名称与分组名称相同，增加计数
							if (currentName === currentGroup.name) {
								currentGroup.count++;
							} else {
								// 给分组的第一个元素添加 count 属性
								ret.data.ds1[currentGroup.startIndex].count = currentGroup.count;
								// 重置分组（如果还有元素）
								if (i < ret.data.ds1.length) {
									currentGroup = {
										name: currentName,
										startIndex: i,
										count: 1
									};
								}
							}
						}
						that.List = ret.data.ds1
						that.mainObject = ret.data.ds[0]
						that.mainObject.InspectionMan = that.mainObject.InspectionMan ? that.mainObject
							.InspectionMan : that.$store.state.UserInfo.EmplName

						that.mainObject.QRMan = that.mainObject.QRMan ? that.mainObject
							.QRMan : that.$store.state.UserInfo.EmplName
						if (that.mainObject.Approve == true) {
							that.readOnlyShow = true
							that.buttonShow = false
						} else {
							that.buttonShow = true
							that.readOnlyShow = false
						}
						that.mainObject.OperateDate = that.mainObject.OperateDate ? that.mainObject.OperateDate
							.replace('T', ' ') : ''
						that.$forceUpdate()
						that.loadingPage = false
					})
				} catch (e) {
					console.log(e)
					this.$utils.hideLoading();
					this.$utils.msg('请求失败请重试')
				}
			},
			changeLevel(data, row) {
				getchangeLevel(data)
				uni.$emit('refreshList');
			},
		}
	}
</script>

<style lang="scss" >
	@import "../css/index.css";

	.content-top-Box {
		font-size: 18px;

		.content-top-Box-view {
			background-color: #fff;
			display: flex;
			justify-content: center;
		}
	}

	.tableText {
		color: blue;
	}

	.table table {
		tr td {
			.newlineDiv1 {
				width: 100%;
				display: flex;
				align-items: center;
				height: 100%;
			}

			input {
				width: 100%;
				background-color: #e7e7e7 !important;
			}
		}

	}

	.loading-page {
		background-color: rgba(0, 0, 0, 0.6) !important;
	}



	.colText_child {
		padding-left: 10rpx;
	}

	.contentBox_bottom {
		font-size: 18px;
		padding: 5rpx 10rpx;

		.contentBox_bottom_view1 {
			display: inline-block;
			width: 20px;

		}

		.contentBox_bottom_view2 {
			padding-left: 15rpx;
			display: flex;
			width: 60%;

			.bottom_view2_view {
				display: flex;
				align-items: center;
				justify-content: space-around;

				input {
					width: 70%;
				}
			}
		}
	}
</style>